<?php
/**
 *
 * (model)
 */
namespace App\Models\SalesReport;

use Illuminate\Database\Eloquent\Model;
class PaymentTNSummary extends Model
{
    protected $table = 'sr_payment_none';

    protected $guarded = [];

    public $timestamps = false;

    //表头公共字段
    public static $_common = [
        'type' => ['title' => '值类型','sumField' => false,'per' => false],
//        'rate_odds' => ['title' => '汇率','sumField' => false,'per' => false],
        'sales_volume' => ['title' => '销量','sumField' => true,'per' => false],
        'sales_amount_rmb' => ['title' => '销售额RMB','sumField' => true,'per' => false],
        'refund_num' => ['title' => '退货数量','sumField' => true,'per' => false],
        'refund_amount_rmb' => ['title' => '退款金额RMB','sumField' => true,'per' => true],
        't_purchase_price_rmb' => ['title' => '总采购成本RMB','sumField' => true,'per' => true],
        't_shipment_price_rmb' => ['title' => '总运费成本RMB','sumField' => true,'per' => true],
        'selling_fees_rmb' => ['title' => '佣金RMB','sumField' => true,'per' => true],
        'fba_fees_rmb' => ['title' => 'FBA交易费RMB','sumField' => true,'per' => true],
        'p_thfba_rmb' => ['title' => '三方发货费RMB','sumField' => true,'per' => true],
        'ms_fee_rmb' => ['title' => '月仓储费RMB','sumField' => true,'per' => true],
        'lts_fee_rmb' => ['title' => '长期仓储费RMB','sumField' => true,'per' => true],
        'p_icfee_rmb' => ['title' => '库存补偿费RMB','sumField' => true,'per' => true],
        'rm_fee_rmb' => ['title' => '移除费用RMB','sumField' => true,'per' => true],
        'rebates_rmb' => ['title' => '促销费RMB','sumField' => true,'per' => true],
        'spend_rmb' => ['title' => '广告费用RMB','sumField' => true,'per' => true],
        'reimburse_rmb' => ['title' => '刷单费用RMB','sumField' => true,'per' => true],
        'reimburse_os_rmb' => ['title' => '运营服务费RMB','sumField' => true,'per' => true],
        'reimburse_profit_rmb' => ['title' => '刷单毛利扣减RMB','sumField' => true,'per' => true],
        'ied_rmb' => ['title' => '退货二次销售RMB','sumField' => true,'per' => true],
        'rod_rmb' => ['title' => '销毁货物损失RMB','sumField' => true,'per' => true],
        'cplan_fee_rmb' => ['title' => '清库计划费用RMB','sumField' => true,'per' => true],
        'vat_fee_rmb' => ['title' => 'VAT费用RMB','sumField' => true,'per' => true],
        'profit_rmb' => ['title' => '毛利值RMB','sumField' => true,'per' => true],
        'profit_rate' => ['title' => '毛利率','sumField' => false,'per' => false],
        'earning_rmb' => ['title' => '销售收入RMB','sumField' => true,'per' => true],
        'total_rmb' => ['title' => 'TotalRMB','sumField' => true,'per' => true],
        'avg_price' => ['title' => '平均售价','sumField' => false,'per' => false],
    ];
    public static function headersField(): array
    {
        return self::$_common;
    }

    //导出数据公共字段-数值类型
    public static function getNumDataCommon($row): array
    {
        return [
            10 => '数值',
            11 => $row->sales_volume,
            12 => $row->sales_amount_rmb,
            13 => $row->refund_num,
            14 => $row->refund_amount_rmb,
            15 => $row->t_purchase_price_rmb,
            16 => $row->t_shipment_price_rmb,
            17 => $row->selling_fees_rmb,
            18 => $row->fba_fees_rmb,
            19 => $row->p_thfba_rmb,
            20 => $row->ms_fee_rmb,
            21 => $row->lts_fee_rmb,
            22 => $row->p_icfee_rmb,
            23 => $row->rm_fee_rmb,
            24 => $row->rebates_rmb,
            25 => $row->spend_rmb,
            26 => $row->reimburse_rmb,
            27 => $row->reimburse_os_rmb,
            28 => $row->reimburse_profit_rmb,
            29 => $row->ied_rmb,
            30 => $row->rod_rmb,
            31 => $row->cplan_fee_rmb,
            32 => $row->vat_fee_rmb,
            33 => $row->profit_rmb,
            34 => getPercentage($row->profit_rmb,$row->sales_amount_rmb),
            35 => $row->earning_rmb,
            36 => $row->total_rmb,
            37 => intval($row->sales_volume) > 0 ? round(($row->sales_amount_rmb + $row->rebates_rmb) / intval($row->sales_volume),3) : '--',
        ];
    }
    //导出数据公共字段-占比类型
    public static function getPerDataCommon($row): array
    {
        return [
            10 => '占比',
            11 => '--',
            12 => '--',
            13 => getPercentage($row->refund_num,$row->sales_volume),
            14 => getPercentage($row->refund_amount_rmb,$row->sales_amount_rmb),
            15 => getPercentage($row->t_purchase_price_rmb,$row->sales_amount_rmb),
            16 => getPercentage($row->t_shipment_price_rmb,$row->sales_amount_rmb),
            17 => getPercentage($row->selling_fees_rmb,$row->sales_amount_rmb),
            18 => getPercentage($row->fba_fees_rmb,$row->sales_amount_rmb),
            19 => getPercentage($row->p_thfba_rmb,$row->sales_amount_rmb),
            20 => getPercentage($row->ms_fee_rmb,$row->sales_amount_rmb),
            21 => getPercentage($row->lts_fee_rmb,$row->sales_amount_rmb),
            22 => getPercentage($row->p_icfee_rmb,$row->sales_amount_rmb),
            23 => getPercentage($row->rm_fee_rmb,$row->sales_amount_rmb),
            24 => getPercentage($row->rebates_rmb,$row->sales_amount_rmb),
            25 => getPercentage($row->spend_rmb,$row->sales_amount_rmb),
            26 => getPercentage($row->reimburse_rmb,$row->sales_amount_rmb),
            27 => getPercentage($row->reimburse_os_rmb,$row->sales_amount_rmb),
            28 => getPercentage($row->reimburse_profit_rmb,$row->sales_amount_rmb),
            29 => getPercentage($row->ied_rmb,$row->sales_amount_rmb),
            30 => getPercentage($row->rod_rmb,$row->sales_amount_rmb),
            31 => getPercentage($row->cplan_fee_rmb,$row->sales_amount_rmb),
            32 => getPercentage($row->vat_fee_rmb,$row->sales_amount_rmb),
            33 => getPercentage($row->profit_rmb,$row->sales_amount_rmb),
            34 => '--',
            35 => getPercentage($row->earning_rmb,$row->sales_amount_rmb),
            36 => getPercentage($row->total_rmb,$row->sales_amount_rmb),
            37 => '--',
        ];
    }
    //导出数值+占比
    public static function getDownloadData($re): \Generator
    {
        foreach ($re as $row){
            yield self::getNumDataCommon($row);
            yield self::getPerDataCommon($row);
        }
    }
    //导出数值
    public static function getDownloadNumData($re): \Generator
    {
        foreach ($re as $row){
            yield self::getNumDataCommon($row);
        }
    }
    //导出占比
    public static function getDownloadPerData($re): \Generator
    {
        foreach ($re as $row){
            yield self::getPerDataCommon($row);
        }
    }
}
